package com.zaofan.controller;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.Recharge;
import com.zaofan.service.RechargeService;
import com.zaofan.utils.JsonUtils;

@WebServlet("/recharge")
public class RechargeController extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private RechargeService rechargeService = new RechargeService();

	protected void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			List<Recharge> recharges = rechargeService.findAll(params);
			Long count = rechargeService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", recharges, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	protected void pay(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String rechargeNo = request.getParameter("rechargeNo");
			rechargeService.payByAdmin(rechargeNo);
			JsonUtils.writeSuccessJson("支付成功!!", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("支付失败", response);
		}
		JsonUtils.writeFailJson("支付失败!!", response);
	}

}
